package graduate.cslg.com.universityshopping.card;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.adapter.CostInfoAdapter;
import graduate.cslg.com.universityshopping.entity.CardCostBean;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class OrderDetailActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private String getUserAccount;
    private List<CardCostBean> mCostBeans;
    private CostInfoAdapter mCostInfoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detial);
        mRecyclerView = findViewById(R.id.cost_detail_recycler);
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315222");
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        getDailyCost(getUserAccount, getNowDate());
    }

    private void getDailyCost(String getUserAccount, String date) {
        RetrofitDevelop.getTestService()
                .getCardCostInfo(getUserAccount, date)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CardCostBean>>() {
                    @Override
                    public void onCompleted() {
                        mCostInfoAdapter = new CostInfoAdapter(mCostBeans);
                        mRecyclerView.setAdapter(mCostInfoAdapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<CardCostBean> cardCostBeans) {
                        mCostBeans = cardCostBeans;
                    }
                });
    }

    public static String getNowDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }
}
